<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Beans.Requests.DataRetrieval.MiscDataRetrievalRequest"%>
<%@page import="Beans.Responses.DataRetrievalComponents.MiscDataComponent"%>
<%@page import="Utilities.Constants"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="org.virtualbox_3_2.DeviceType"%>
<%@page import="Utilities.Constants.VirtualResourcesRetrievalLimit"%>
<%@page import="Managers.Utilities.User"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Utilities.Functions"%>
<%@page import="Beans.Requests.DataRetrieval.MediaDataRetrievalRequest"%>
<%@page import="Beans.Responses.DataRetrievalComponents.MediumDataComponent"%>
<%@page import="Beans.Responses.DataRetrievalResponse"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);

    //data retrieval requests
    MediaDataRetrievalRequest dataRequest = null;
    MiscDataRetrievalRequest mediumFormatsRequest = null;

    //data retrieval responses
    DataRetrievalResponse<MediumDataComponent> data = null;
    DataRetrievalResponse<MiscDataComponent> mediumFormatsData = null;

    //target form field name, when the media page is opened for medium selection
    String targetFieldName = request.getParameter("targetFieldName");

    //attempts to retreive and parse the medium type
    DeviceType mediaType = null;
    int parameterMediaType = 0;
    try
    {
        parameterMediaType = Integer.parseInt(request.getParameter("type"));
        switch(parameterMediaType)
        {
            case 1: mediaType = DeviceType.HARD_DISK; break;
            case 2: mediaType = DeviceType.DVD; break;
            case 3: mediaType = DeviceType.FLOPPY; break;
            default: ;
        }
    }
    catch(NumberFormatException e)
    {}

    //attempts to retreived a medium UUID, if supplied
    String mediumId = request.getParameter("id");

    //attempts to retrieve an owner id, if supplied
    int ownerId = Constants.INVALID_USER_ID;
    try
    {
        ownerId = Integer.parseInt(request.getParameter("ownerId"));
    }
    catch(NumberFormatException e)
    {}

    //checks if a proper media type was supplied
    if(mediaType != null)
    {
        if(Functions.isUUIDValid(mediumId))
        {
            //a single medium is to be retrieved
            dataRequest = new MediaDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                        userSessionData.getCurrentServerId(),
                                                        mediumId,
                                                        mediaType);
        }
        else if(Functions.isUserIdValid(ownerId))
        {
            //all media owned by the specified user are to be retrieved
            dataRequest = new MediaDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                        userSessionData.getCurrentServerId(),
                                                        ownerId,
                                                        mediaType);
        }
        else
        {
            //all available media are to be retrieved
            dataRequest = new MediaDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                        userSessionData.getCurrentServerId(),
                                                        mediaType);

            mediumFormatsRequest = new MiscDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                        userSessionData.getCurrentServerId(),
                                                        Constants.MiscellaneousRequestType.MEDIUM_FORMATS);
        }

        //attempts to submit the request(s) and retreives the response(s)
        if(UIFunctions.sendServiceRequest(request, response, dataRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
        {
            data = (DataRetrievalResponse<MediumDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
            
            if(UIFunctions.sendServiceRequest(request, response, mediumFormatsRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
                mediumFormatsData = (DataRetrievalResponse<MiscDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
            else
                ;
        }
        else
            ;
    }
    else //invalid or no media type; only page menu is displayed
        ;
%>
<p:printPageHeader pageName="Media Management" pageId="pageMedia" userSessionData="<%= userSessionData %>"/>
    <ul class="navigationBar">
        <li <%= (parameterMediaType == 1) ? "id='currentSub'" : "" %>><a href="media.jsp?type=1">Hard Disks</a></li>
        <li <%= (parameterMediaType == 2) ? "id='currentSub'" : "" %>><a href="media.jsp?type=2">DVDs</a></li>
        <li <%= (parameterMediaType == 3) ? "id='currentSub'" : "" %>><a href="media.jsp?type=3">Floppy Disks</a></li>
    </ul>

    <jstlc:choose>
        <jstlc:when test="<%= mediaType != null %>">
            <p:printMedia data="<%= data %>" type="<%= parameterMediaType %>" 
                          targetFieldName="<%= targetFieldName %>"
                          mediumFormatsData="<%= mediumFormatsData %>"/>
        </jstlc:when>

        <jstlc:otherwise>
            <p>Please, select media type.</p>
        </jstlc:otherwise>
    </jstlc:choose>
<p:printPageFooter/>